{% extends "layout.html" %}
{% block title %}文件管理{% endblock %}

{% block head %}
<style>
    .upload-form {
        background-color: #eaf3f9;
        padding: 20px;
        border-radius: 8px;
        margin-bottom: 30px;
        display: flex;
        gap: 15px;
        align-items: center;
    }
    .upload-form input[type="file"] {
        flex-grow: 1;
    }
    .upload-form button {
        background-color: #28a745;
        color: white;
        border: none;
        padding: 10px 20px;
        border-radius: 5px;
        cursor: pointer;
        font-size: 1rem;
        transition: background-color 0.2s ease;
    }
    .upload-form button:hover {
        background-color: #218838;
    }
    .file-list {
        list-style-type: none;
        padding: 0;
    }
    .file-list li {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 15px;
        border: 1px solid #e0e0e0;
        border-radius: 8px;
        margin-bottom: 10px;
        background-color: #fdfdfd;
    }
    .file-list .file-name {
        font-weight: bold;
        color: #0056b3;
        word-break: break-all;
    }
    .file-list .file-actions {
        display: flex;
        flex-shrink: 0;
    }
    .file-list .file-actions a, .file-list .file-actions button {
        text-decoration: none;
        color: #fff;
        padding: 8px 12px;
        border-radius: 5px;
        margin-left: 10px;
        font-size: 0.9em;
        transition: background-color 0.2s ease;
        border: none;
        cursor: pointer;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }
    .file-list .file-actions .view-btn {
        background-color: #007bff;
    }
    .file-list .file-actions .view-btn:hover {
        background-color: #0056b3;
    }
    .file-list .file-actions .edit-btn {
        background-color: #ffc107;
        color: #333;
    }
    .file-list .file-actions .edit-btn:hover {
        background-color: #e0a800;
    }
    .file-list .file-actions .copy-btn {
        background-color: #17a2b8;
    }
    .file-list .file-actions .copy-btn:hover {
        background-color: #138496;
    }
    .file-list .file-actions .delete-btn {
        background-color: #dc3545;
    }
    .file-list .file-actions .delete-btn:hover {
        background-color: #c82333;
    }
</style>
{% endblock %}

{% block content %}
<h1>文件管理</h1>

<!-- Upload Form -->
<form action="{{ url_for('upload_file') }}" method="post" enctype="multipart/form-data" class="upload-form">
  <input type="file" name="file">
  <button type="submit">上传文件</button>
</form>

<!-- File List -->
<h2>已上传文件</h2>
{% if files %}
    <ul class="file-list">
        {% for file in files %}
        <li>
            <span class="file-name">{{ file }}</span>
            <span class="file-actions">
                <a href="{{ url_for('view_file', filename=file) }}" class="view-btn">查看</a>
                <a href="{{ url_for('edit_file', filename=file) }}" class="edit-btn">修改</a>
                <button class="copy-btn" data-filename="{{ file }}">复制链接</button>
                <form action="{{ url_for('delete_file', filename=file) }}" method="post" style="display: inline; margin-left: 10px;" onsubmit="return confirm('确定要删除这个文件吗？');">
                    <button type="submit" class="delete-btn">删除</button>
                </form>
            </span>
        </li>
        {% endfor %}
    </ul>
{% else %}
    <p>还没有上传任何文件。</p>
{% endif %}
{% endblock %}

{% block scripts %}
<script>
    async function copyToClipboard(text, button) {
        const originalText = button.textContent;
        try {
            if (navigator.clipboard && window.isSecureContext) {
                await navigator.clipboard.writeText(text);
            } else {
                const textarea = document.createElement('textarea');
                textarea.value = text;
                textarea.style.position = 'fixed';
                textarea.style.left = '-9999px';
                document.body.appendChild(textarea);
                textarea.focus();
                textarea.select();
                document.execCommand('copy');
                document.body.removeChild(textarea);
            }
            button.textContent = '已复制!';
        } catch (err) {
            console.error('复制失败:', err);
            button.textContent = '复制失败';
        } finally {
            setTimeout(() => {
                button.textContent = originalText;
            }, 2000);
        }
    }

    document.addEventListener('DOMContentLoaded', () => {
        const copyButtons = document.querySelectorAll('.copy-btn');
        copyButtons.forEach(button => {
            button.addEventListener('click', (e) => {
                const filename = e.target.dataset.filename;
                if (filename) {
                    // Construct the full URL for the file
                    const fileUrl = `${window.location.origin}{{ url_for('uploads_file', filename='placeholder') }}`.replace('placeholder', filename);
                    copyToClipboard(fileUrl, e.target);
                }
            });
        });
    });
</script>
{% endblock %}